# -*- coding: utf-8 -*-

'''
    date:2017-05-31 hqk
    
'''

import redis
import json
import time
import requests, urllib
import os
from config.setting import SPIDERSENSUS_REDIS, RESTINFO_REDIS,RESTNEW, RESTUPDATE, PROXIE_UPDATETIME_KEY, RESTPICKEY, RESTINFOKEY, RESTNEWADDINFO, RESTNEWADDPIC, RESTUPDATEINFO, RESTUPDATEPIC, REST_LIST_KEY, REST_SET_KEY, RESTNEWADDREVIEW, RESTUPDATEREVIEW, RESTBILLBOADKEY, RESTREVIEWKEY, RESTCLOSE

import sys  
reload(sys)  
sys.setdefaultencoding('utf8')
from dianp_log import DianpLog

COMMITBASEURL = ''

Host = 'http://10.47.56.146:24897/RestDataReptileService'
Host2 = 'http://10.47.106.75:24897/RestDataReptileService'

class DianpCensus():
    def __init__(self):
        self.log_restinfo = DianpLog('_restinfo.log')
        self.log_dish = DianpLog('_restdish.log')
        self.log_billboad = DianpLog('_restbollboad.log')
        self.log_reviewlabel = DianpLog('_restreviewlabel.log')
        self.log_review = DianpLog('_restreview.log')
        self.log_pic = DianpLog('_restpic.log')
        
        self.pool = redis.ConnectionPool(host=SPIDERSENSUS_REDIS['host'], port=SPIDERSENSUS_REDIS['port'], db=SPIDERSENSUS_REDIS['db'])
        self.rdsCensus = redis.Redis(connection_pool=self.pool)
        
        RESTINFO_REDIS
        self.infopool = redis.ConnectionPool(host=RESTINFO_REDIS['host'], port=RESTINFO_REDIS['port'], db=RESTINFO_REDIS['db'])
        self.rdsInfo = redis.Redis(connection_pool=self.infopool)
            
            
    def __restinfo_fix(self, restid, restInfo):
        restInfoJ = restInfo
        oldRestInfo = self.rdsInfo.get(RESTINFOKEY(restid))
        if oldRestInfo != None:
            oldRestInfo = json.loads(oldRestInfo)
            for k,v in restInfoJ.items():
                if v != None:
                    oldRestInfo[k] = v
            NewRestInfo = json.dumps(oldRestInfo)
        else:
            NewRestInfo = restInfo
        self.rdsInfo.set(RESTINFOKEY(restid), NewRestInfo)
    
    def __pic_fix(self, restid, picInfo):
        for item in picInfo:
            pids = item['pids']
            for pid in pids:
                self.rdsInfo.sadd(RESTPICKEY(restid), pid)

    def __review_fix(self, restid, reviewInfo):
        for first in reviewInfo:
            reviewid = first['id']
            self.rdsInfo.set(RESTREVIEWKEY(restid), reviewid)

    def __billboad_fix(self, restid, billboad):
        for item in billboad:
            billid = item['listId']
            self.rdsInfo.set(RESTBILLBOADKEY(restid), billid)

    def __rest_close(self, restid):
        pass

    def commit_rest_close(self):
        closearr = self.rdsCensus.lpop(RESTNEWADDINFO)
        while closearr:
            dataJ = json.loads(update)
            restid = dataJ['id']
            self.__rest_close(restid)
            self.rdsCensus.srem(REST_LIST_KEY, restid)

    def commit_newadd_rest_info(self):
        update = self.rdsCensus.lpop(RESTNEWADDINFO)
        while update:
            #self.rdsCensus.lpush(RESTNEWADDINFO, update)
            dataJ = json.loads(update)
            restid = dataJ['id']
            if dataJ['data']['recommendFood'] != []:
                self.sync_dianp_dish(restid, dataJ['data']['recommendFood'])
            if dataJ['data']['userThink'] != []:
                self.sync_dianp_review_label(restid, dataJ['data']['userThink'])
            if dataJ['data']['billBoad'] != []:
                self.sync_dianp_billboad(restid, dataJ['data']['billBoad'])
                self.__billboad_fix(restid, dataJ['data']['billBoad'])
            self.sync_dianp_restinfo(restid, dataJ['data']['restinfo'])
            #self.__restinfo_fix(restid, dataJ['data'])
            self.rdsCensus.sadd(REST_LIST_KEY, restid)
            #self.rdsCensus.lpush(REST_SET_KEY, restid)
            update = self.rdsCensus.lpop(RESTNEWADDINFO)
        
    def commit_newadd_rest_pic(self):
        update = self.rdsCensus.lpop(RESTNEWADDPIC)
        print update
        while update:
            #self.rdsCensus.lpush(RESTNEWADDPIC, update)
            dataJ = json.loads(update)
            restid = dataJ['id']
            #self.__pic_fix(restid, dataJ['data'])
            self.sync_dianp_pic(restid, dataJ['data'])
            update = self.rdsCensus.lpop(RESTNEWADDPIC)

    def commit_newadd_rest_review(self):
        update = self.rdsCensus.lpop(RESTNEWADDREVIEW)
        while update:
            dataJ = json.loads(update)
            restid = dataJ['id']
            #self.__review_fix(restid, dataJ['data'])
            self.sync_dianp_review(restid, dataJ['data'])
            update = self.rdsCensus.lpop(RESTNEWADDREVIEW)
            
    def commit_update_rest_info(self):
        update = self.rdsCensus.lpop(RESTUPDATEINFO)
        while update:
            #self.rdsCensus.lpush(RESTUPDATEINFO, update)
            dataJ = json.loads(update)
            restid = dataJ['id']
            changeFlag = 0
            if dataJ['data']['recommendFood'] != None:
                changeFlag = changeFlag + 1
                self.sync_dianp_dish(restid, dataJ['data']['recommendFood'])
                print 'sync_dianp_dish'
            if dataJ['data']['userThink'] != None:
                changeFlag = changeFlag + 1
                self.sync_dianp_review_label(restid, dataJ['data']['userThink'])
                print 'sync_dianp_review_label'
            if dataJ['data']['billBoad'] != None:
                changeFlag = changeFlag + 1
                self.sync_dianp_billboad(restid, dataJ['data']['billBoad'])
                print 'sync_dianp_billboad'
                #self.__billboad_fix(restid, dataJ['data']['billBoad'])
            if dataJ['data']['restinfo'] != None:
                changeFlag = changeFlag + 1
                self.sync_dianp_restinfo(restid, dataJ['data']['restinfo'])
                logStr = 'restid:[' + restid + '] update restinfo :[' + json.dumps(dataJ['data']['diffKeys']) + ']'
                self.log_restinfo.Log(logStr)
                print 'sync_dianp_restinfo'
            #if changeFlag > 0:
                #self.__restinfo_fix(restid, dataJ['data'])
            update = self.rdsCensus.lpop(RESTUPDATEINFO)
        
    def commit_update_rest_pic(self):
        update = self.rdsCensus.lpop(RESTUPDATEPIC)
        while update:
            #self.rdsCensus.lpush(RESTUPDATEPIC, update)
            dataJ = json.loads(update)
            restid = dataJ['id']
            self.__pic_fix(restid, dataJ['data'])
            self.sync_dianp_pic(restid, dataJ['data'])
            update = self.rdsCensus.lpop(RESTUPDATEPIC)
       
    def commit_update_rest_review(self):
        update = self.rdsCensus.lpop(RESTUPDATEREVIEW)
        while update:
            dataJ = json.loads(update)
            restid = dataJ['id']
            #self.__review_fix(restid, dataJ['data'])
            self.sync_dianp_review(restid, dataJ['data'])
            update = self.rdsCensus.lpop(RESTUPDATEREVIEW)
    
    def __restInfo_request(self, params):
        parmasX = urllib.urlencode(params)
        requests.get(COMMITBASEURL + parmasX)
     
    def __restPic_request(self, params):
        parmasX = urllib.urlencode(params)
        requests.get(COMMITBASEURL + parmasX)
    
    
    def rest_newadd_info(self, restid, data):
        jsons = self.census_type(restid, data)
        data_json = json.dumps(jsons)
        self.rdsCensus.lpush(RESTNEWADDINFO, data_json)
        
    #@staticmethod
    def rest_newadd_pic(self, restid, data):
        jsons = self.census_type(restid, data)
        data_json = json.dumps(jsons)
        self.rdsCensus.lpush(RESTNEWADDPIC, data_json)

    def rest_newadd_review(self, restid, data):
        jsons = self.census_type(restid, data)
        data_json = json.dumps(jsons)
        self.rdsCensus.lpush(RESTNEWADDREVIEW, data_json)

    #@staticmethod
    def rest_update_info(self, restid, data):
        jsons = self.census_type(restid, data)
        data_json = json.dumps(jsons)
        self.rdsCensus.lpush(RESTUPDATEINFO, data_json)
        
    #@staticmethod
    def rest_update_pic(self, restid, data):
        jsons = self.census_type(restid, data)
        data_json = json.dumps(jsons)
        self.rdsCensus.lpush(RESTUPDATEPIC, data_json)

    def rest_update_review(self, restid, data):
        jsons = self.census_type(restid, data)
        data_json = json.dumps(jsons)
        self.rdsCensus.lpush(RESTUPDATEREVIEW, data_json)

    def rest_close(self, restid, data):
        jsons = self.census_type(restid, data)
        data_json = json.dumps(jsons)
        self.rdsCensus.lpush(RESTCLOSE, data_json)
        
    def update_proxie_times(self):
        self.rdsCensus.incr(PROXIE_UPDATETIME_KEY)
           
    def get_update_times(self):
        return self.rdsCensus.get(PROXIE_UPDATETIME_KEY)
        
        
    def census_type(self, restid, data):
        now = time.strftime('%Y-%m-%d',time.localtime(time.time()))
        jsons = {}
        jsons['date'] = now
        jsons['id'] = restid
        jsons['data'] = data
        return jsons
    
    def params_handle(self, item):
        for k,v in item.items():
            if v == None:
                item[k] = ''
        
    def sync_dianp_dish(self, restid, jsonData):
        for item in jsonData:
            if item['price'] == None:
                item['price'] = 0
            params = {
                        "ResId": int(restid), 
                        "DishName": item['name'],
                        "DishScore": item['count'],
                        "DishPrice": int(item['price']),
                        "DishLabel": item['foodLabel'], 
                        "NumberOfPicGot": 0,
                        "PicIds":None, 
                        }
            request = {"request":params}
            reqUrl = Host + '/SyncDianpingDish?'
            logStr = 'restid:[' + restid + '] update dish :[' + str(item['name']) + ']'
            self.log_dish.Log(logStr)
            print json.dumps(request)
            re = requests.post(reqUrl, data=json.dumps(request))
            state, content = self.parse_result(re.content)
            while state == False:
                print content
                time.sleep(2)
                re = requests.post(reqUrl, json.dumps(request))   
                state, content = self.parse_result(re.content)
            
    def sync_dianp_review(self, restid, jsonData):
        for item in jsonData:
           params = {}
           try:
            params = {
                    'Id':item['id'],
                    'ResId':int(restid),
                    'CommentContent':item['commentInfo'],
                    'UserName':item['userName'],
                    'CommentDate':item['time'],
                    'Star':int(item['star']),
                    'Taste':int(item['taste']),
                    'Srnd':float(item['environment']),
                    'Service':float(item['service']),
                    'Avrg':0,
                    'Dish':item['others']
                      }
           except:
                print 'error restid : ', int(restid)
           logStr = 'restid:[' + restid + '] update review :[' + str(item['id']) + ']'
           self.log_review.Log(logStr)
           request = {"request":params}
           print request
           reqUrl = Host + '/SyncDianpingComment?'
           re = requests.post(reqUrl, json.dumps(request))
           state, content = self.parse_result(re.content)
           while state == False:
                print content
                time.sleep(2)
                re = requests.post(reqUrl, json.dumps(request))   
                state, content = self.parse_result(re.content)
            
    def sync_dianp_review_label(self, restid, jsonData):
        print jsonData
        for item in jsonData:
            params = {
                    'ResId':int(restid),
                    'LabelName':item['tag'],
                    'Count':int(item['count']),
                      }
            request = {"request":params}
            logStr = 'restid:[' + restid + '] update reviewlabel :[' + str(item['tag']) + ']'
            self.log_reviewlabel.Log(logStr)
            reqUrl = Host + '/SyncDianPingCommentLabel?'
            re = requests.post(reqUrl, json.dumps(request))
            state, content = self.parse_result(re.content)
            while state == False:
                print content, request
                time.sleep(2)
                re = requests.post(reqUrl, json.dumps(request))
                state, content = self.parse_result(re.content)
            

    def sync_dianp_pic(self, restid, jsonData):
        for item in jsonData:
            tag = item['tag']
            if tag == '全部图片':
                tag = '其他'
            for item2 in item['pids']:
                print item2['url']
                params = {
                        'ResId':restid,
                        'PicId':item2['id'],
                        'Title':item2['intro'],
                        'MainType':tag,
                        'SubType': u'其他',
                        'PicUrl': os.path.join(restid, tag, item2['id']+'.jpg'),
                        'OssPath':os.path.join(restid, tag, item2['id'])
                        }
                request = {"request":params}
                reqUrl = Host2 + '/SyncDianpingPic?'
                logStr = 'restid:[' + restid + '] update pic :[' + str(item2['id']) + ']'
                self.log_pic.Log(logStr)
                re = requests.post(reqUrl, json.dumps(request))
                state, content = self.parse_result(re.content)
                while state == False:
                    print content
                    time.sleep(2)
                    re = requests.post(reqUrl, json.dumps(request))
                    state, content = self.parse_result(re.content)

    def get_dianp_restinfo(self, restid):
        reqUrl = Host + '/GetDianpingById?resId=' + restid
        re = requests.get(reqUrl)
        return re.content

    def check_restInfo(self, restid, newInfo):
        diffKeys = []
        data = self.get_dianp_restinfo(restid)
        dataJ = json.loads(data)
        oldInfo = dataJ['value']
        newInfo['XMSId'] = oldInfo['XMSId']
            
        if newInfo['shopName'] != oldInfo['ResName']:
                diffKeys.append('ResName')
        if newInfo['altName'] != oldInfo['AltName']:
                diffKeys.append('AltName')
        if newInfo['cusineId'] != oldInfo['CusineId']:
                diffKeys.append('CusineId')
        if newInfo['secondCusineId'] != oldInfo['SecondCusineId']:
                diffKeys.append('SecondCusineId')
        if newInfo['districtId'] != oldInfo['DistrictId']:
                diffKeys.append('DistrictId')
        if int(newInfo['star']) != oldInfo['Star']:
                diffKeys.append('Star')
        if float(newInfo['taste']) != oldInfo['Taste']:
                diffKeys.append('Taste')
        if float(newInfo['environment']) != oldInfo['Env']:
                diffKeys.append('Env')
        if float(newInfo['service']) != oldInfo['Service']:
                diffKeys.append('Service')
        if float(newInfo['lngs'][:-1]) != oldInfo['Lon']:
                diffKeys.append('Lon')
        if float(newInfo['lats'][:-1]) != oldInfo['Lat']:
                diffKeys.append('Lat')
        if int(newInfo['chainId']) != oldInfo['ChainId']:
                diffKeys.append('ChainId')
        if int(newInfo['avgPriceTitle']) != oldInfo['Avrg']:
                diffKeys.append('Avrg')
        if newInfo['tel'] != oldInfo['ResTel']:
                diffKeys.append('ResTel')
        if newInfo['openTime'] != oldInfo['BusinessHour']:
                diffKeys.append('BusinessHour')
        if newInfo['crossRoad'] != oldInfo['CrossRoad']:
                diffKeys.append('CrossRoad')
        print diffKeys

        if len(diffKeys) == 0:
            return True, diffKeys
        else:
            return False, diffKeys

    def sync_dianp_billboad(self, restid, jsonData):
        for data in jsonData:
            params = {
                    'Id':data['listId'],
                    'ResId':int(restid),
                    'Title':data['title'],
                    'BrowseTimes':data['viewCount'],
                    'ResCount':data['shopCount'],
                    }
            request = {"request":params}
            print request
            reqUrl = Host + '/SyncSelectedList?'
            logStr = 'restid:[' + restid + '] update billboad :[' + str(data['listId']) + ']'
            self.log_billboad.Log(logStr)
            re = requests.post(reqUrl, json.dumps(request))
            state, content = self.parse_result(re.content)
            while state == False:
                print content
                time.sleep(2)
                re = requests.post(reqUrl, json.dumps(request))
                state, content = self.parse_result(re.content)

    def parse_result(self, content):
        content = json.loads(content)
        if content['code'] != 200:
            return False, content
        else:
            return True, content
         
        
    def sync_dianp_restinfo(self, restid, jsonData):
        print jsonData
        if jsonData['mainRegionId'] == '':
            jsonData['mainRegionId'] = 0
        if int(jsonData['districtId'][1:]) == int(jsonData['mainRegionId']):
            jsonData['mainRegionId'] = 0
        if jsonData.has_key('XMSId') == False:
            jsonData['XMSId'] = ''
        params = {
                'ResId':int(restid),
                'CityId':jsonData['cityId'],
                'ResName':jsonData['shopName'],
                'BranchName':None,
                'AltName':jsonData['altName'],
                'XMSId': jsonData['XMSId'],
                'CusineId':jsonData['cusineId'],
                'SecondCusineId':jsonData['secondCusineId'],
                'DistrictId': jsonData['districtId'],
                'BussinessAreaId': int(jsonData['mainRegionId']),
                'BuildingId': 0,
                'Star': int(jsonData['star']),
                'Taste': float(jsonData['taste']),
                'Env': float(jsonData['environment']),
                'Service': float(jsonData['service']),
                'Addr': jsonData['address'],
                'Lon': float(jsonData['lngs']),
                'Lat': float(jsonData['lats']),
                'ChainId': jsonData['chainId'],
                'Avrg': int(jsonData['avgPriceTitle']),
                'ResTel': jsonData['tel'],
                'BusinessHour': jsonData['openTime'],
                'IsDeleted': 0,
                'ResStatus': '',
                'CrossRoad': jsonData['crossRoad'],
                'IsLonLatWrong': 0,
                }
        request = {"request":params}
        print request
        reqUrl = Host2 + '/SyncDianpingRes?'
        re = requests.post(reqUrl, json.dumps(request))
        state, content = self.parse_result(re.content)
        while state == False:
            print content
            time.sleep(2)
            re = requests.post(reqUrl, json.dumps(request))
            state, content = self.parse_result(re.content)
        resJ = json.loads(re.content)
        if jsonData['XMSId'] == '':
            jsonData['XMSId'] = resJ['value']['XMSId']

#xx = DianpCensus() 
#print xx.get_dianp_restinfo('14743854')
#xx.commit_update_rest_review()
#xx.commit_update_rest_info()
#xx.check_restInfo('500001', '')
#xx.commit_newadd_rest_info()
#xx.commit_newadd_rest_pic()
#xx.commit_newadd_rest_review()
